<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller\m1;
use App\Model\Manager;
use Hyperf\Di\Annotation\Inject;
use Qbhy\HyperfAuth\Annotation\Auth;
use Qbhy\HyperfAuth\AuthManager;
use App\Constants\ReturnCode;
use App\Controller\AbstractController;
use Hyperf\DbConnection\Db;
use Throwable;
class ManagerController extends AbstractController
{
    #[Inject]
    protected AuthManager $auth;
    /**
     * 管理后台用户登录
     *
     * @return void
     */
    public function login(){
        $username = $this->request->input('username');
        $password = $this->request->input('password');
        if(empty($username) || empty($password)){
            return $this->fmt_response(ReturnCode::para_miss,'账号或密码不能为空');
        }
        $row = Manager::query()->select('id','username','group_id','extend_permission','status')
        ->where(['is_del'=>0,'username'=>$username,'password'=>md5($password)])
        ->first();
        if(empty($row)){
            return $this->fmt_response(ReturnCode::para_error,'账号或密码错误');
        }
        $token = $this->auth->login($row);
        $data = $row->toArray();
        if($data['status'] !== 1){
            return $this->fmt_response(ReturnCode::para_error,'账号已被锁定，请联系管理员');
        }
        return $this->fmt_response(ReturnCode::deal_succ,'登陆成功',['token'=>$token]);
    }
    /**
     * 管理后台用户退出登录
     *
     * @return void
     */
    public function logout(){
        $token = $this->request->input('token');
        if(empty($token)){
            return $this->fmt_response(ReturnCode::token_miss);
        }
        if($this->auth->check($token)){
            $this->auth->logout($token);
        }
        return $this->fmt_response(ReturnCode::deal_succ,'退出成功');
    }
    /**
     * 管理后台用户个人信息
     *
     * @return void
     */
    public function user(){
        $token = $this->request->input('token');
        if(empty($token)){
            return $this->fmt_response(ReturnCode::token_miss);
        }
        if($this->auth->check($token)){
            $result = $this->auth->user($token);
            $data = [
                'id'=>(int)$result->id,
                'group_id'=>(int)$result->group_id,
                'username'=>(string)$result->username,
            ];
            //获取用户组权限
            $group_data = Db::table('permission_group')->select('group_name','permission')->where('id',$data['id'])->first();
            //用户扩展权限转数组
            $permission_user = [];
            if(!empty($result->extend_permission)){
                $permission_user = explode(',',$result->extend_permission);
            }
            //用户组权限转数组
            $permission_group = [];
            if(!empty($group_data['permission'])){
                $permission_group = explode(',',$group_data['permission']);
            }
            $permission = array_values(array_unique(array_merge($permission_user,$permission_group)));
            if(!empty($permission)){
                $permission_info = Db::table('permission')->select('function_name','permission_name','function_code','path_info')->whereIn('id', $permission)->get();
            }
            $data['permission_info'] = !empty($permission_info) ? $permission_info->toArray() : [];
            return $this->fmt_response(ReturnCode::deal_succ,'获取成功',$data);
        }else{
            return $this->fmt_response(ReturnCode::token_error);
        }
    }
}